<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/include/page.jsp"%>
<script type="text/javascript">
/*
 * {"id":"2108eacb802b4eada17484d27ffde141","department":"001","level":1,"order":0,"name":"办公室","parentid":"super"},
 
 * [{"id":"2108eacb802b4eada17484d27ffde141","name":"办公室"}]
 */
/* 默认选中的效果 */
<c:set value="" var="defSel"/>
var dataTreeJSON = [
	<c:forEach items="${requestScope.menuTreeList }" var="menu" varStatus="stat">
		{"id":"${menu.id}","checkStaStr":"${menu.checkStaStr}","name":"${menu.name }","pubTime":"<fmt:formatDate value="${menu.pubTime }" pattern="yyyy-MM-dd HH:mm:ss"/>",
			"parentid":${menu.parentId == 0 ? 'null' : menu.parentId}}
		<c:if test="${!stat.last }">,</c:if>
		<c:if test="${menu.checkStaStr =='1'}">
			<c:set value="${defSel}${stat.count - 1}," var="defSel"/>
		</c:if>
	</c:forEach>
]; 

var defSel = "${defSel}";

// 操作列
function datagrid_tree_operation(value,data) 
{
    var html = "<a href='${rootPath }/back/admins/menuInsert.htm?parentId=" + data.id + 
	"' data-toggle='navtab' data-options=\"{id:'menuInsert',title:'添加子节点'}\" type='button' " + 
	" class='btn btn-green' data-icon='edit'> <i class='fa fa-edit'></i> 添加子节点 </a>"+
	"<a href='${rootPath }/back/admins/menuUpdate.htm?operType=updateInfo&rid=" + data.id + 
    	"' data-toggle='navtab' data-options=\"{id:'menuUpdate',title:'菜单更新'}\" type='button' " + 
    	" class='btn btn-green' data-icon='edit'> <i class='fa fa-edit'></i> 编辑 </a>";
    return html
}

/* 复选框 */
function checkSubmit()
{
	var rows = $("#j_datagrid_tree").data('selectedTrs');
	if(rows == undefined)
	{
		BJUI.alertmsg('error', '至少选择一条记录');
		return false; 
	}
	/* debugger; */
	/* alert(rows.length); */
	/* 取到所有复选框的值 */
	var sb = "roleId=${param.rid}&"; 
	rows.each(function()
	{
		sb += "menuId=" + $(this).children("td").eq(2).text() + "&";
	});
	/* alert(sb) */
	/* ajax提交给后台 */
	$.post(
		"${rootPath}/back/admins/roleMenuInsertSubmit.htm",
		sb,
		function(data)
		{
			BJUI.alertmsg('ok', data.message);
		},
		"json"
	);
	return false ; 
}
</script>
<div class="bjui-pageContent">
	<div id="checkSubmit">
		<c:if test="${param.rid != null && param.rid != '' }">
			<button type="button" class="btn-default" onclick="return checkSubmit()">分配权限</button>
		</c:if>
	</div>
	
    <table id="j_datagrid_tree" data-toggle="datagrid" data-options="{
        width: '100%',
        height: '100%',
        gridTitle: '菜单树形结构',
		showToolbar: true,
		toolbarCustom:'#checkSubmit',
        local: 'local',
        data:dataTreeJSON,
        inlineEditMult: false,
        isTree: 'name',
        addLocation: 'last',
        columnMenu: false,
		<c:if test="${!(param.rid == null || rid == '')}">
	        showCheckboxcol:'lock',
		</c:if>
        paging: false,
        treeOptions: {
            add: false,
            simpleData: true,
            isExpand:false,
            keys: {
                parentKey: 'parentid'
            }
        }
    }">
        <thead>
            <tr>
            	<th data-options="{name:'id', align:'center',width:50}">id</th>
                <th data-options="{name:'name', align:'center', width:300, rule:'required'}">角色名称</th>
                <th data-options="{name:'pubTime', width:300}">发布时间</th>
                <th data-options="{render:datagrid_tree_operation}">操作列</th>
            </tr>
        </thead>
    </table>
</div>
<script type="text/javascript">
	window.setTimeout(function()
	{
		$("#j_datagrid_tree").datagrid("selectedRows",defSel,true);
	},500);
</script>